/****************************************************************
 * <strong>MFTransaction事务控制类</strong>
 *
 * <p>
 * 事务控制类
 * </p>
 *
 * @author AndyZhang 2011 cnet2001@163.com
 * @version 0.1
 * ToDo:
 ***************************************************************/

package com.thinkwide.data.ormapping;

import com.thinkwide.data.ormapping.db.MFConnectionInfo;
import com.thinkwide.data.ormapping.db.MFDataManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;

/**
 * 事务管理
 */
public class MFTransaction {
    private static Logger logger = LoggerFactory.getLogger(MFTransaction.class);
    private String transactionName;

    private Connection conn;

    private MFConnectionInfo connInfo;

    public MFTransaction(String dbName, String transName) {
        conn = MFDataManager.getConnection(dbName);
        connInfo = MFDataManager.getConnectionInfo(dbName);
        if (conn == null) {
            logger.error("Not Found:" + dbName);
        }
        transactionName = transName;
    }

    public MFTransaction(String dbName) {
        conn = MFDataManager.getConnection(dbName);
        connInfo = MFDataManager.getConnectionInfo(dbName);
        if (conn == null) {
            logger.error("Not Found:" + dbName);
        }
    }

    public MFTransaction() {
        connInfo = MFDataManager.getConnectionInfo();
        conn = MFDataManager.getConnection();
    }

    public MFTransaction(Connection conn, MFConnectionInfo connInfo) {
        this.connInfo = connInfo;
        this.conn = conn;
    }

    public void free() throws SQLException {
        conn.close();
    }

    public void setTransactionName(String transactionName) {
        this.transactionName = transactionName;
    }

    public String getTransactionName() {
        return transactionName;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public Connection getConn() {
        return conn;
    }

    public void close() {
        try {
            conn.close();
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        }
    }

    public void commit() {
        try {
            conn.commit();
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        }
    }

    public void rollback() {
        try {
            conn.rollback();
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        }
    }

    public void rollback(Savepoint point) {
        try {
            conn.rollback(point);
        } catch (SQLException e) {

            logger.error(e.getMessage(), e);
        }
    }

    public Savepoint setSavepoint(String pointName) {
        try {
            return conn.setSavepoint(pointName);
        } catch (SQLException e) {

            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public void beginTransaction() {
        try {
            if (conn.getAutoCommit()) {
                conn.setAutoCommit(false);
            }
        } catch (SQLException e) {

            logger.error(e.getMessage(), e);
        }
    }

    public String getDbName() {
        return connInfo.getDbName();
    }

    public String getDbType() {
        return connInfo.getDbType();
    }

    public String getDbVersion() {
        return connInfo.getDbVersion();
    }

    public String getAdapterClasses() {
        return connInfo.getAdapterClasses();
    }

    public String getDriverClasses() {
        return connInfo.getDriverClasses();
    }

    public String getUrl() {
        return connInfo.getUrl();
    }

    public String getPassword() {
        return connInfo.getPassword();
    }

    public String getUser() {
        return connInfo.getUser();
    }
}
